#!/bin/bash
. ${ATLASEDGE_SOFTWARE_PATH}/edge_site/edge_manager/src/script/log_util.sh

##########################################################################################
#   FUNCTION   : main
#   DESCRIPTION: check cert, chooes edge-installer or edge-register
#   CALLED BY  :
##########################################################################################
main()
{
    local certPath=$1
    launcher_mode=$2
    launcher_args=$3
    logger_Info "#################check the Cert#################"
    uninstallRetryTime=3
    while [ "$uninstallRetryTime" != 0 ]
    do
        if [ "$launcher_mode" = "FD_IEF" ]; then
            cd /opt/edge-installer; sudo ./installer -op=uninstall -args="$launcher_args"
        else
            cd /opt/edge-installer; sudo ./installer -op=uninstall
        fi
        if [ $? -ne 0 ]; then
            logger_Error "edge_install uninstall failed!"
        fi

        edge_count=`ps -ef |grep edge_core |grep -v "grep" |grep -v "AtlasEdge" |grep -v "edge_core.log" |wc -l`
        if [ 0 != $edge_count ]; then
            logger_Info "Uninstall, wait next time uninstall"
            sleep 10s
        else
            logger_Info "###############Uninstall Success####################"
            break
        fi
        ((uninstallRetryTime--))
    done
    edge_count=`ps -ef |grep edge_core |grep -v "grep" |grep -v "AtlasEdge" |grep -v "edge_core.log" |wc -l`
    if [ 0 != $edge_count ]; then
        logger_Error "edge_install uninstall failed, process is exit!"
        exit 1
    fi
    if [ -d /opt/Atlastmp/IEF ];then
        rm -rf /opt/Atlastmp/IEF
    fi
    mkdir -p /opt/Atlastmp/IEF
    chmod 700 /opt/Atlastmp/IEF
    tar -zxf $certPath -C /opt/Atlastmp/IEF
    chown HwHiAiUser:HwHiAiUser -R /opt/Atlastmp/IEF

    #check IEF valid
    if [ -f /opt/Atlastmp/IEF/register ] || [ -f /opt/Atlastmp/IEF/version ] || [ -d /opt/Atlastmp/IEF/conf ];then
        logger_Error "invalid files in Cert.tar.gz"
        rm -rf /opt/Atlastmp/IEF
        exit 1
    fi 

    logger_Info "#################Install########################"
    if [ -f /opt/Atlastmp/IEF/user_config ];then
        mkdir -p /opt/IEF/Cert
        chmod 755 /opt/IEF
        cp -rf /opt/Atlastmp/IEF/* /opt/IEF/Cert
        if [ "$launcher_mode" = "FD_IEF" ]; then
            cd /opt/edge-installer; sudo ./installer -op=install -args="$launcher_args"
        else
            cd /opt/edge-installer; sudo ./installer -op=install
        fi

        if [ $? -ne 0 ]; then
                logger_Error "edge_install install failed!"
                exit 1
        fi
        edge_count=`ps -ef |grep edge_core |grep -v "grep" |grep -v "AtlasEdge" |grep -v "edge_core.log" |wc -l`
        if [ 0 = $edge_count ];then
            logger_Error "edge_install install failed, the process is not exit!"
            exit 1
        fi
    else
        if [ "$launcher_mode" = "FD_IEF" ]; then
            ls /opt/Atlastmp/IEF/*.tar.gz > /dev/null 2>&1
            if [ $? -eq 0 ]; then
                tar -zxf /opt/Atlastmp/IEF/*.tar.gz -C /opt/Atlastmp/IEF/
                if [ $? -ne 0 ]; then
                    logger_Error "edge_register install failed, please check the format of cert!"
                    rm -rf /opt/Atlastmp/IEF/
                    exit 1
                fi
                rm -f /opt/Atlastmp/IEF/*.tar.gz
            else
                echo "{\"enable_npu\":true,\"log_configs\":[{\"component\":\"system\",\"type\":\"LTS\",\"level\":\"info\",\"size\":10,\"rotate_num\":5,\"rotate_period\":\"daily\"},{\"component\":\"app\",\"type\":\"LTS\",\"level\":\"on\",\"size\":10,\"rotate_num\":5,\"rotate_period\":\"daily\"}]}" > /opt/edge-register/nodeinfo
            fi
            sudo cp -rf /opt/Atlastmp/IEF/* /opt/edge-register/
            cd /opt/edge-register; sudo ./register --mode="cert" --installer-args="$launcher_args"
        else
            sudo cp -rf /opt/Atlastmp/IEF/* /opt/edge-register/
            echo "{\"enable_npu\":true,\"log_configs\":[{\"component\":\"system\",\"type\":\"LTS\",\"level\":\"info\",\"size\":10,\"rotate_num\":5,\"rotate_period\":\"daily\"},{\"component\":\"app\",\"type\":\"LTS\",\"level\":\"on\",\"size\":10,\"rotate_num\":5,\"rotate_period\":\"daily\"}]}" > /opt/edge-register/nodeinfo
            cd /opt/edge-register; sudo ./register --mode="cert"
        fi
        if [ $? -ne 0 ]; then
            logger_Error "edge_register install failed!"
            exit 1
        fi
        edge_count=`ps -ef |grep edge_core |grep -v "grep" |grep -v "AtlasEdge" |grep -v "edge_core.log" |wc -l`
        if [ 0 = $edge_count ];then
            logger_Error "edge_register install failed, the process exit!"
            exit 1
        fi
    fi
    logger_Info "###############Install Success###################"
    return 0
}

LOG_DIR=$1
main $2 $3 $4
exit $?

